﻿package com.tudou.ad.view
{
    import com.tudou.ad.components.*;
    import com.tudou.ad.components.events.*;
    import com.tudou.ad.events.*;
    import com.tudou.ad.view.base.*;
    import com.tudou.skin.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;

    public class AdPauseView extends AdVideoViewBase
    {
        protected var backgroundV1:DisplayObject;
        protected var allowShowNearEnd:Boolean = false;
        protected var videoPadding:int = 3;
        protected var skipBtn:SimpleButton;
        private var hasInitViewListen:Boolean = false;

        public function AdPauseView()
        {
            return;
        }// end function

        override protected function adShowNearEnd() : void
        {
            if (!allowShowNearEnd)
            {
                return;
            }
            dispatchEvent(new AdItemViewEvent(AdItemViewEvent.SHOW_NEAR_END));
            return;
        }// end function

        protected function clearBgv() : void
        {
            if (backgroundV1)
            {
                viewBox.removeChild(backgroundV1);
            }
            backgroundV1 = null;
            return;
        }// end function

        override public function initToShow() : void
        {
            if (!initToShowCommon())
            {
                return;
            }
            isVideo = adItemInfo.isVideo;
            videoWidth = mainWidth - 6;
            videoHeight = mainHeight - 23;
            showAdAtStart = true;
            initBgv();
            if (isVideo)
            {
                initVideoStuff();
            }
            initFrontStuff();
            if (isVideo)
            {
                initControlPanel();
            }
            initSkipBtn();
            if (adVideo)
            {
                adVideo.volume = 0;
                adVideo.bufferMaxTimes = 0;
                adVideo.timeout = 0;
            }
            else
            {
                allowShowNearEnd = true;
            }
            return;
        }// end function

        private function viewBoxClickHlr(event:MouseEvent) : void
        {
            if (event.target == skipBtn)
            {
            }
            else
            {
                adClick();
            }
            return;
        }// end function

        protected function clearSkipBtn() : void
        {
            if (skipBtn)
            {
                skipBtn.removeEventListener(MouseEvent.CLICK, skipHlr);
                viewBox.removeChild(skipBtn);
            }
            skipBtn = null;
            return;
        }// end function

        protected function floatClickHlr(event:Event) : void
        {
            adFloatClick();
            return;
        }// end function

        override protected function swfCompleteHlr(event:TudouAdSpriteEvent) : void
        {
            return;
        }// end function

        override protected function clearControlPanel() : void
        {
            if (controlSprite)
            {
                controlSprite.removeEventListener(AdVideoControlPanelEvent.TURN_ON, volumeHlr);
                controlSprite.removeEventListener(AdVideoControlPanelEvent.TURN_OFF, volumeHlr);
                controlSprite.removeEventListener(AdVideoControlPanelEvent.FLOAT_CLICK, floatClickHlr);
                controlSprite.clear();
                viewBox.removeChild(controlSprite);
            }
            controlSprite = null;
            return;
        }// end function

        override protected function initControlPanel() : void
        {
            controlSprite = new AdPauseVideoControlPanel();
            viewBox.addChild(controlSprite);
            controlSprite.width = mainWidth;
            controlSprite.volumeOn = false;
            controlSprite.addEventListener(AdVideoControlPanelEvent.TURN_ON, volumeHlr);
            controlSprite.addEventListener(AdVideoControlPanelEvent.TURN_OFF, volumeHlr);
            controlSprite.addEventListener(AdVideoControlPanelEvent.FLOAT_CLICK, floatClickHlr);
            return;
        }// end function

        override protected function adShowComplete() : void
        {
            if (!isSkip)
            {
                _adShowComplete();
            }
            if (isSkip && adItemInfo.isVideo)
            {
                clearAd();
                return;
            }
            if (!adItemInfo.isVideo)
            {
                return;
            }
            clearControlPanel();
            clearVideoStuff();
            allowShowNearEnd = true;
            adShowNearEnd();
            if (frontAdSprite)
            {
                frontAdSprite.visible = true;
            }
            initViewListen();
            return;
        }// end function

        override protected function _showAd() : void
        {
            super._showAd();
            if (!isVideo)
            {
                initViewListen();
            }
            return;
        }// end function

        protected function initBgv() : void
        {
            backgroundV1 = RectDisplayUtil.getShape(mainWidth, mainHeight, 0, _frontAdSpriteAlpha);
            viewBox.addChild(backgroundV1);
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            _loc_4 = (param1 - bgWidth) / 2;
            _loc_5 = (param2 - bgHeight) / 2;
            if (backgroundV1)
            {
                backgroundV1.x = _loc_4;
                backgroundV1.y = _loc_5;
            }
            if (frontAdSprite)
            {
                frontAdSprite.x = _loc_4;
                frontAdSprite.y = _loc_5;
            }
            if (adVideo)
            {
                adVideo.x = _loc_4 + videoPadding;
                adVideo.y = _loc_5 + videoPadding;
            }
            if (controlSprite)
            {
                controlSprite.x = _loc_4 + videoPadding;
                controlSprite.y = _loc_5 + videoHeight + videoPadding;
            }
            if (skipBtn)
            {
                skipBtn.x = _loc_4 + videoWidth - 17;
                skipBtn.y = _loc_5 + videoPadding + 7;
            }
            return;
        }// end function

        protected function initSkipBtn() : void
        {
            skipBtn = new CloseBTN();
            viewBox.addChild(skipBtn);
            skipBtn.addEventListener(MouseEvent.CLICK, skipHlr);
            return;
        }// end function

        override protected function frontStuffClickHlr(event:TudouAdSpriteEvent) : void
        {
            return;
        }// end function

        override protected function clearComps() : void
        {
            clearControlPanel();
            clearVideoStuff();
            clearFrontStuff();
            clearSkipBtn();
            clearViewListen();
            clearBgv();
            allowShowNearEnd = false;
            clearCompsCommon();
            return;
        }// end function

        protected function clearViewListen() : void
        {
            if (!hasInitViewListen)
            {
                return;
            }
            if (viewBox)
            {
                viewBox.removeEventListener(MouseEvent.CLICK, viewBoxClickHlr);
            }
            hasInitViewListen = false;
            return;
        }// end function

        protected function initViewListen() : void
        {
            if (hasInitViewListen)
            {
                return;
            }
            hasInitViewListen = true;
            if (viewBox)
            {
                viewBox.addEventListener(MouseEvent.CLICK, viewBoxClickHlr);
            }
            return;
        }// end function

    }
}
